/*

  Mean of darks

  Copyright © 2010-2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "munipack.h"
//#include <wx/cmdline.h>
//#include <iostream>
//#include <cstring>
//#include <cstdio>
//#include <cerrno>
//#include <cstdlib>


//using namespace std;


bool Munipack::mdark(MuniCmdLineParser& cmd)
{
  //  action = new MuniAction(this,"mdark",true);
  MuniProcess *action = new MuniProcess(&pipe,"mdark");
  //  MuniProcess *action = new MuniProcess(&pipe,"cat");
  pipe.push(action);

  wxString a;


  /*
  wxString a;

  if( cmd.Found("o",&a) )
    action->AddInput("Output filename = '" + a + "'");

  if( cmd.Found("b",&a) )
    action->AddInput("Output BITPIX = " + a);
  */  

  if( cmd.Found("r") )
    action << wxString("Robust mean = F");
  else
    action << wxString("Robust mean = T");

  if( cmd.Found("o",&a) )
    action << ("Output filename = '" + a + "'");

  if( cmd.Found("b",&a) )
    action << ("Output BITPIX = " + a);

  action->Write(cmd.GetParams());

  //  fitskeys();


  //  action->AddCommonInput(cmd);
  //  action->AddFilesInput(cmd);

  /*
  bool stdin = false;
  for(size_t i = 1; i < cmd.GetParamCount(); i++) {
    if( cmd.GetParam(i) == "-" )
      stdin = true;
    else
      action->AddInput("'" + cmd.GetParam(i) + "'");
  }
    
  if( stdin ) {
    char line[32768];
    while( true ) {
      cin.getline(line,32768);
      if( ! cin.good() ) break;
      action->AddInput("'" + wxString(line) + "'");
    }
  }
  */
  //  action->CloseOutput();

  pipe.Start();

  return true;

   
  // //   break;

  // // default:
    
  // //   // cmd.Usage() is called by default
  // //   return false;

  // // };

  // /*
  // if( cmd.GetParamCount() <= 1 ) {
  //   cmd.Usage();
  //   return false;
  // }
  // */

  // wxLogDebug("# %d",(int) cmd.GetParamCount());
  // for(size_t i = 0; i < cmd.GetParamCount(); i++)
  //   wxLogDebug(cmd.GetParam(i));

  // // for(int i = 0; i < argc; i++)
  // //   wxLogDebug(argv[i]);


  // // wxCmdLineParser cmd(argc,argv);

  // // cmd.AddParam("mdark",wxCMD_LINE_VAL_STRING);
  // // cmd.AddSwitch("r",wxEmptyString,"robust statistical estimator");
  // // cmd.AddParam("file(s)",wxCMD_LINE_VAL_STRING,wxCMD_LINE_PARAM_OPTIONAL|wxCMD_LINE_PARAM_MULTIPLE);

  // // //  app->OnInitCmdLine(cmd);

  // // cmd.Parse();
  // // wxLogDebug("%d",(int) cmd.GetParamCount());


  // // if( cmd.GetParamCount() <= 0 ) {
  // //   cmd.Usage();
  // //   return false;
  // // }

  // return false;




  // // init, help
  // int arg0 = argc;
  // for(int i = 1; i < argc; i++) {

  //   if( strcmp(argv[i],"mdark") == 0 ) 
  //     arg0 = i + 1;

  //   if( strcmp(argv[i],"--help") == 0 || arg0 == argc ) {
  //     cout << "munipack mdark [-o file] [-b bitpix] [-r] file(s)" 
  // 	   << endl;
  //     return 0;
  //   }

  // }

  // // open and load pipe
  // FILE *p;
  // if( (p = popen("mdark","w")) == NULL ) {
  //   cerr << strerror(errno) << endl;
  //   return errno;
  // }

  // fitskeys(p);

  // for(int i = arg0; i < argc; i++) {

  //   if( strcmp(argv[i],"-o") == 0 && i++ < argc) 
  //     fprintf(p,"Output filename = '%s'\n",argv[i]);

  //   else if( strcmp(argv[i],"-b") == 0 && i++ < argc) 
  //     fprintf(p,"Output BITPIX = %s\n",argv[i]);

  //   else if( strcmp(argv[i],"-r") == 0 ) 
  //     fprintf(p,"Robust mean = T\n");

  //   else if( strcmp(argv[i],"-") == 0 ) {
  //     char line[32768];
  //     while( true ) {
  // 	cin.getline(line,32768);
  // 	if( ! cin.good() ) break;
  // 	fprintf(p,"'%s'\n",line);
  //     }
  //   }
    
  //   else
  //     fprintf(p,"'%s'\n",argv[i]);

  // }
  // pclose(p);

  // return errno;
}
